//
//  ECMaterailManageListViewController.m
//  ECERP.CRM
//
//  Created by huanggulong on 2018/11/25.
//  Copyright © 2018 升鲜宝. All rights reserved.
//

#import "ECMaterailManageListViewController.h"
#import "GLBaseHTTPTool.h"
#import "ECShopMaterial.h"
#import "ECMaterailMangeTableViewCell.h"

@interface ECMaterailManageListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,assign) NSInteger pageIndex;

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataArray;

@end

@implementation ECMaterailManageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsZero);
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    ECMaterailMangeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.material = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 136;
}

#pragma mark - network
- (void)loadNewData{
    self.pageIndex = 1;
    GLHTTPParam *param = [GLHTTPParam param];
    [param setSafeValue:@(1) forKey:@"page"];
    [param setSafeValue:@(20) forKey:@"limit"];
    [param setSafeValue:self.materialTypeId forKey:@"inMaterialTypeIds"];
    [param setSafeValue:self.enabled forKey:@"enabled"];
    
    [GLBaseHTTPTool getRequestURL:Api_MaterialMaterialPageURL requestParam:param resultModelType:[ECShopMaterialResult class] success:^(ECShopMaterialResult * result) {
        [self.tableView.mj_header endRefreshing];
        if (result.code == 0) {
            if (result.data.count > 0) {
                self.pageIndex = 2;
            }
            self.dataArray = [NSMutableArray arrayWithArray:result.data];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showText:result.msg?:@"拉取数据失败" toView:self.view];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD showText:@"拉取数据失败" toView:self.view];
    }];
}

- (void)loadMoreData{
    GLHTTPParam *param = [GLHTTPParam param];
    [param setSafeValue:@(self.pageIndex) forKey:@"page"];
    [param setSafeValue:@(20) forKey:@"limit"];
    [param setSafeValue:self.materialTypeId forKey:@"inMaterialTypeIds"];
    [param setSafeValue:self.enabled forKey:@"enabled"];
    
    [GLBaseHTTPTool getRequestURL:Api_MaterialMaterialPageURL requestParam:param resultModelType:[ECShopMaterialResult class] success:^(ECShopMaterialResult * result) {
        [self.tableView.mj_footer endRefreshing];
        if (result.status == 1) {
            if (result.data.count > 0) {
                self.pageIndex ++;
                [self.dataArray addObjectsFromArray:result.data];
                [self.tableView reloadData];
            }
        }else{
            [MBProgressHUD showText:result.msg?:@"拉取数据失败" toView:self.view];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD showText:@"拉取数据失败" toView:self.view];
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 27 * PX_RELATIVE375;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        UINib *nib = [UINib nibWithNibName:@"ECMaterailMangeTableViewCell" bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
